﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace EXPrinter
{
    public class PARAM
    {
        private static bool m_bSetNode;
        private static int m_nParamCount;
        xdDict Dic;

        private static string m_strErrInfo;
        private static string m_strVer;


        public PARAM()
        {
            m_bSetNode = false;
            m_nParamCount = 0;
            Dic = new xdDict();
        }

        
        #region InputParamString
        public int InputParamString(string strParam)
        {
            int nTmp;
            int i = 0;
            string[] strArrary1 = strParam.Split('.');

            int nCount1 = strArrary1.Length;
            if (nCount1 < 3)
            {
                m_strErrInfo = "Can't find the info(Ver.parameters name.parameter data)";
                return -1;	//数据格式不合法.
            }

            string strVer = strArrary1[0];
            string strNames = strArrary1[1];
            //20131225	JYSH
            string strDatas; strDatas = strArrary1[2];
            for (i = 3; i < nCount1; i++)	//防止后面的数据中有'.'符号，将数据截断了.
            {
                strDatas += ("." + strArrary1[i]);
            }


            //获取版本信息
            if (strVer.Length != 7)
            {
                m_strErrInfo = "Can't find the Version info(Ver####)";
                return -1;
            }

            m_strVer = strVer.Substring(3, 4);

            //获取参数名称和长度
            ArrayList strArrNames = new ArrayList();
            string[] strArrayNames = strNames.Split(';');
            int nNameCount = strArrayNames.Length-1;
            int nDataCount; nDataCount = 0;
            int[] nParamLen = new int[128];
            for (i = 0; i < nNameCount; i++)
            {
                string[] strArrNameLen = strArrayNames[i].Split(':');
                nTmp = strArrNameLen.Length;
                if (2 != nTmp)
                {
                    m_strErrInfo = string.Format("Can't find the Parameter name and parameter data len info(Name:Len) \r\n {0}", strArrayNames[i]);
                    return -1;
                }

                strArrNames.Add(strArrNameLen[0]);
                nParamLen[i] = int.Parse(strArrNameLen[1]); 
                if (0 == nParamLen[i])
                {
                    if ("0" != strArrNameLen[1])
                    {
                        m_strErrInfo = string.Format("Can't find the Parameter name and parameter data len info(Name:Len) \r\n {0}", strArrayNames[i]);
                        return -1;
                    }
                }

                nDataCount += nParamLen[i];
            }

            //李锦泽备注
            int byteLen = System.Text.Encoding.Default.GetByteCount(strDatas);
            //if ((nNameCount + nDataCount) != (strDatas.Length))
            if ((nNameCount + nDataCount) != (byteLen))
            {
                m_strErrInfo = string.Format("The parameter data is dismatch,nDataCount[{0}],nNameCount[{1}],Data Len[{2}]", nDataCount, nNameCount, strDatas.Length);
                return -1;
            }

            //char* cTemp; cTemp = new char[strDatas.GetLength()];
            //memcpy(cTemp, strDatas.GetBuffer(0), strDatas.GetLength());

            Dic.SetNode(nNameCount, 512);
            int nOffset = 0;
            for (i = 0; i < nNameCount; i++)
            {
                //if (";" != strDatas.SubstringEx(nParamLen[i] + nOffset, 1))
                if (";" != SubstringEx(strDatas,nParamLen[i] + nOffset, 1))
                {
                    //m_strErrInfo = string.Format("Can't find the separating character ':',[{0}]", strDatas.Substring(nParamLen[i] + nOffset, 1));

                    //delete cTemp; cTemp = NULL;
                    return -1;
                }

                if (nParamLen[i] > 512)
                {
                    m_strErrInfo=string.Format("The Value is too long ,[{0}]", nParamLen[i]);

                    //delete cTemp; cTemp = NULL;
                    return -1;
                }

                //Dic.AddNode(i + 1, strArrNames[i].ToString(), strArrNames[i].ToString().Length, strDatas.Substring(nOffset, nParamLen[i]), nParamLen[i]);
                Dic.AddNode(i + 1, strArrNames[i].ToString(), strArrNames[i].ToString().Length, SubstringEx(strDatas,nOffset, nParamLen[i]), nParamLen[i]);
                nOffset += (nParamLen[i] + 1);
            }

            //delete cTemp; cTemp = NULL;

            m_nParamCount = nNameCount;
            return 0;
        }
        #endregion

        public int SetVer(string  strVer)	//设置版本信息
        {
	        m_strVer = strVer;
	        return 0;
        }

        public string GetVer()
        {
            return m_strVer;
        }

        public int SetParam(string ParamName, string PararmVal)	//设置参数(输入参数名称、参数数据)
        {
            
	        if(!m_bSetNode)
	        {
                Dic.SetNode(128, 512);
		        m_bSetNode=true;
		        m_nParamCount =0;
	        }

	        m_nParamCount++;
            //byte[] byteParamName = System.Text.Encoding.Default.GetBytes(ParamName);
            byte[] bytePararmVal = System.Text.Encoding.Default.GetBytes(PararmVal);
            Dic.InsertNode(m_nParamCount, ParamName, ParamName.Length, PararmVal, bytePararmVal.Length);
            //Dic.InsertNode(m_nParamCount, ParamName, ParamName.Length, PararmVal, PararmVal.Length);

	        return 0;
        }

        public string GetParamString()
        {
	        string strInfo;
            strInfo = "";
	        string	strVer,strNames,strVals,strName,strVal,strNameLen;
	        int nLen;

            strNames = ""; strVals = "";
	        string cVal="";
	        strVer = string.Format("Ver{0}",m_strVer);
	        for (int i=0;i<m_nParamCount;i++)
	        {
		        //memset(cVal,0,sizeof(cVal));
		        strName=Dic.GetNodeAlias(i+1);
                nLen = Dic.GetValueStr(i + 1, ref cVal);

		        strNameLen = string.Format("{0}:{1}",strName,nLen);
		        strVal=string.Format("{0}",cVal);

		        strNames += (strNameLen + ";");
		        strVals += (strVal + ";");
	        }

	        strInfo = string.Format("{0}.{1}.{2}",strVer,strNames,strVals);
	        return strInfo;
        }


        public string GetParamVal(string strName)						//返回参数数据(输入参数ID,或者输入参数名称)
        {
	        string	strAlias;strAlias="";
	        strAlias =Dic.GetValueStr(strName);

	        return strAlias;
        }

        public static string SubstringEx(string s, int nStartIndex,int length)
        {
            byte[] bytes = System.Text.Encoding.Default.GetBytes(s);
            int n = 0;  //  表示当前的字节数
            int i = 0;  //  要截取的字节数
            //int nChnCnt=0;
            //计算字符串的中文字数
            //for (int j = 0; j < bytes.GetLength(0);j++ )
            //{
            //    if (j%2!=0)
            //    {
            //        if (bytes[j]>0)
            //        {
            //            nChnCnt++;
            //        }
            //    }
            //}
            byte[] newByte = new byte[length];
            for (i = nStartIndex; n < length ; i++)
            {
                newByte[n] = bytes[i];
                n++;
            }
            //for (i = nStartIndex; i < bytes.GetLength(0) && n < length; i++)
            //{
            //    //  偶数位置，如0、2、4等，为UCS2编码中两个字节的第一个字节
            //    if (i % 2 == 0)
            //    {
            //        n++;      //  在UCS2第一个字节时n加1
            //        newByte[n] = bytes[i];
            //    }
            //    else
            //    {
            //       //  当UCS2编码的第二个字节大于0时，该UCS2字符为汉字，一个汉字算两个字节
            //        if (bytes[i] > 0)
            //        {
            //            n++;
            //        }
            //    }
            //}

            ////  如果i为奇数时，处理成偶数
            //if (i % 2 == 1)
            //{ 
            //     //  该UCS2字符是汉字时，去掉这个截一半的汉字

            //    if (bytes[i] > 0)
            //        i = i - 1;

            //     //  该UCS2字符是字母或数字，则保留该字符
            //    else
            //        i = i + 1;
            //}
            return System.Text.Encoding.Default.GetString(newByte, 0, length);
        }
    }
}
